﻿using System;
using System.Drawing;
using System.Threading.Tasks;
using System.Windows.Forms;
using NDK.Module.Model;

namespace NDK.Frm
{
    public partial class Dlg_Sample : Form
    {
        private Point m_Offset;
        public Dlg_Sample()
        {
            InitializeComponent();

            this.StartPosition = FormStartPosition.Manual;
            int x = SystemInformation.PrimaryMonitorSize.Width / 2 - this.Width / 2;
            int y = SystemInformation.PrimaryMonitorSize.Height / 2 - this.Height / 2;
            this.Location = new Point(x, y);
        }
        #region UI

       
       
        //响应确认
        private void lbl_ConfirmOk_MouseEnter(object sender, EventArgs e)
        {
            lbl_ConfirmOk.BackColor = Color.FromArgb(174, 218, 151);
        }
        private void lbl_ConfirmOk_MouseLeave(object sender, EventArgs e)
        {
            lbl_ConfirmOk.BackColor = Color.White;
        }
        private void lbl_ConfirmOk_Click(object sender, EventArgs e)
        {
            if (cb_OpenSample.CheckState == CheckState.Checked)
            {

                Common.sampleCount = int.Parse(nud_SampleCount.Value.ToString());
                if (Common.sampleCount <= 0)
                {
                    lbl_Tip.Text = "采样数不能小于等于0";
                    return;
                }
                Common.samplePersent = int.Parse(nud_SamplePercent.Value.ToString());
                if (Common.samplePersent <= 0 || Common.samplePersent > 100)
                {
                    lbl_Tip.Text = "采样概率必须在1到100之间";
                    return;
                }
                lbl_Tip.Text = "";
                Common.isSample = true;
                Common.curSampleCount = 0;
            }
            else
            {
                lbl_Tip.Text = "采样关闭";
                Task.Delay(3000).Wait();
                Common.isSample = false;
                Common.samplePersent = 0;
                Common.curSampleCount = 0;
                Common.sampleCount = 0;
            }
            this.DialogResult = DialogResult.OK;
            if (Common.isSample)
            {
                lbSamplePercent.Text = string.Format("抽样比例: {0}%", Common.samplePersent);
                lbSampleCount.Text = string.Format("抽样数: {0}", Common.sampleCount);
                lbLoaclCount.Text = string.Format("当前采样数量: {0}", Common.curSampleCount);
            }
            else
            {
                lbSamplePercent.Text = "当前抽检未开启";
                lbSampleCount.Text = "当前抽检未开启";
                lbLoaclCount.Text = "当前抽检未开启";
            }
        }
        //响应取消
        private void lbl_ConfirmCancel_MouseEnter(object sender, EventArgs e)
        {
            lbl_ConfirmCancel.BackColor = Color.FromArgb(174, 218, 151);
        }
        private void lbl_ConfirmCancel_MouseLeave(object sender, EventArgs e)
        {
            lbl_ConfirmCancel.BackColor = Color.White;
        }
        private void lbl_ConfirmCancel_Click(object sender, EventArgs e)
        {

            this.DialogResult = DialogResult.Cancel;
        }

        #region 窗体拖动
        private void lbl_Title_MouseDown(object sender, MouseEventArgs e)
        {
            if (MouseButtons.Left != e.Button) return;
            Point cur = this.PointToScreen(e.Location);
            m_Offset = new Point(cur.X - this.Left, cur.Y - this.Top);
        }
        private void lbl_Title_MouseMove(object sender, MouseEventArgs e)
        {
            if (MouseButtons.Left != e.Button) return;
            Point cur = MousePosition;
            this.Location = new Point(cur.X - m_Offset.X, cur.Y - m_Offset.Y);
        }
        #endregion

        #endregion

        private void Dlg_Sample_Load(object sender, EventArgs e)
        {
            if (Common.isSample)
            {
                lbSamplePercent.Text = string.Format("抽样比例: {0}%", Common.samplePersent);
                lbSampleCount.Text = string.Format("抽样数: {0}", Common.sampleCount);
                lbLoaclCount.Text = string.Format("当前采样数量: {0}", Common.curSampleCount);
            }
            else
            {
                lbSamplePercent.Text = "当前抽检未开启";
                lbSampleCount.Text = "当前抽检未开启";
                lbLoaclCount.Text = "当前抽检未开启";
            }
        }
    }
}
